<!-- 登录页面 -->
<template>
  <transition
    appear
    name="animate__animated animate__bounce"
    enter-active-class="animate__lightSpeedInLeft"
    leave-active-class="animate__fadeOutDownBig"
  >
    <div class="login-container">
      <div class="login">
        <h2 class="form-title">即刻登录</h2>

        <el-form
          :model="userInfoForm"
          status-icon
          :rules="rules"
          ref="userInfoForm"
        >
          <el-form-item prop="username">
            <el-input
              v-model.trim="userInfoForm.username"
              placeholder="用户名"
              autocomplete="off"
            ></el-input>
          </el-form-item>
          <el-form-item prop="password">
            <el-input
              type="password"
              v-model.trim="userInfoForm.password"
              placeholder="密码"
              autocomplete="off"
            ></el-input>
          </el-form-item>
          <el-form-item>
            <div class="btn" @click="submit('userInfoForm')">Login</div>
          </el-form-item>
        </el-form>
        <div class="register">
          <router-link to="/home">首页</router-link>
          <router-link to="/register">注册</router-link>
        </div>
      </div>
    </div>
  </transition>
</template>

<script>
import { mapState } from "vuex";
import { login } from "@/api/user.js";
import { setItem } from "../../utils/storage";
export default {
  name: "Login",
  data() {
    const username = (rule, val, callback) => {
      if (val === "") {
        return callback(new Error("用户名不能为空"));
      } else if (val.length <= 3) {
        return callback(new Error("用户名太短了"));
      } else callback();
    };
    const password = (rule, val, callback) => {
      if (val === "") {
        callback(new Error("请输入密码"));
      } else if (val.length < 4) return callback(new Error("密码太短了"));
      else {
        callback();
      }
    };
    return {
      userInfoForm: {
        username: "",
        password: "",
      },
      rules: {
        username: [{ validator: username, trigger: "blur" }],
        password: [{ validator: password, trigger: "blur" }],
      },
    };
  },
  methods: {
    getUser() {
      if (this.user) {
        Object.assign(this.userInfoForm, this.user);
      }
    },
    submit(formName) {
      this.$refs[formName].validate(async (valid) => {
        if (valid) {
          try {
            if (!this.user.token) {
              console.log(this.userInfoForm);
              let { data } = await login(this.userInfoForm);
              console.log(data);
              if (data.status === 501) return this.$message.error(data.msg);
              if (data.status === 200) {
                this.$message({
                  type: "success",
                  message: "登陆成功",
                });
                setItem("user", data.data);
                this.$store.commit("UPDATE_USER", data.data);
                this.$router.replace("/");
              }
            } else this.$message.error("您已经登录了");
          } catch (error) {
            this.$message.error("登陆失败");
            console.log(error);
          }
        } else {
          this.$message.error("用户名或密码错误");
          return false;
        }
      });
    },
  },
  created() {
    this.getUser();
  },
  computed: {
    ...mapState(["user"]),
  },
};
</script>

<style lang="less" scoped>
.login-container {
  width: 100%;
  overflow-x: hidden;
  height: 100vh;
  position: relative;
  background: url("../../assets/bj.jpg") no-repeat;
  background-size: 100% 100%;
  .form-title {
    font-weight: 300;
    color: white;
  }
  .login {
    width: 300px;
    height: 350px;
    position: absolute;
    top: 50%;
    bottom: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    text-align: center;
    background-color: rgba(255, 255, 255, 0.1);
    backdrop-filter: blur(5px);
    z-index: 1;
    border-radius: 8px;
    box-shadow: 0 20px 40px rgb(0 0 0 / 10%);
    border: 1px solid rgba(255, 255, 255, 0.2);
    border-right-color: rgba(255, 255, 255, 0.1);
    border-bottom-color: rgba(255, 255, 255, 0.1);
    display: flex;
    flex-flow: column;
    align-items: center;

    .el-form {
      width: 260px;
      height: 260px;
      display: flex;
      flex-flow: column;
      justify-content: space-between;
      .el-form-item {
        /deep/.el-input__inner {
          height: 50px;
          line-height: 50px;
          background-color: transparent;
          border-radius: 8px;
          background-color: transparent;
          border-color: rgba(255, 255, 255, 0.5);
          color: #fff;
          font-size: 15px;
        }
      }

      .btn {
        line-height: 50px;
        width: 100%;
        border-radius: 8px;
        border: none;
        height: 50px;
        background-image: linear-gradient(
          to right,
          #02aab0 0,
          #00cdac 51%,
          #02aab0 100%
        );
        background-size: 200% auto;
        color: #fff;
        box-shadow: 0 20px 40px rgb(0 0 0 / 10%);
        transition: ease 0.4s;
      }
      .btn:hover {
        background-color: rgb(228, 115, 22);
      }
    }
    .register {
      width: 50%;
      display: flex;
      margin-bottom: 10px;
      justify-content: space-evenly;
    }
    .register a {
      color: white;
      font-size: 13px;
    }
    .register a:hover {
      color: chocolate;
    }
  }
}
</style>